package com.yhp.web;

import com.fasterxml.jackson.core.json.JsonReadContext;
import com.yhp.bo.CommentBO;
import com.yhp.pojo.Items;
import com.yhp.pojo.ItemsImg;
import com.yhp.pojo.ItemsParam;
import com.yhp.pojo.ItemsSpec;
import com.yhp.service.ItemService;
import com.yhp.utils.JSONResult;
import com.yhp.utils.PagedGridResult;
import com.yhp.vo.CommentCountsVO;
import com.yhp.vo.ItemInfoVO;
import com.yhp.vo.ShopCartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Api(value = "商品接口",tags = {"商品详情展示"})
@RestController
@RequestMapping("/items")
public class ItemController  extends BaseController{

    @Autowired
    private ItemService itemService;

    //查询购物车商品的信息(用于用户长时间未登陆网站，刷新购物车中的数据，如价格是否有变动)
    //类似于京东
    @GetMapping("/refresh")
    @ApiOperation(value = "查询购物车商品的信息",notes = "查询购物车商品的信息",httpMethod = "GET")
    public JSONResult refresh(
            @ApiParam(name = "itemSpecIds",value = "规格id拼接的字符串",required = true)
            String itemSpecIds){
        if(StringUtils.isBlank(itemSpecIds)){
            return JSONResult.ok();
        }
        List<ShopCartVO> shopCartVOS = itemService.queryItemsBySpecIds(itemSpecIds);
        return  JSONResult.ok(shopCartVOS);
    }


    //查询商品信息
    @GetMapping("/info/{itemid}")
    @ApiOperation(value = "商品信息查询",notes = "商品信息查询",httpMethod = "GET")
    public JSONResult getItem(
            @ApiParam(name = "itemid",value = "商品id",required = true)
            @PathVariable("itemid")String itemid){
        if(StringUtils.isBlank(itemid)){
            return JSONResult.errorMsg("商品id不允许为null");
        }
        Items items = itemService.queryItemById(itemid);
        List<ItemsImg> images = itemService.getImages(itemid);
        ItemsParam param = itemService.getParam(itemid);
        List<ItemsSpec> spec = itemService.getSpec(itemid);

        ItemInfoVO itemInfoVO = new ItemInfoVO(items, images, spec,param);
        return JSONResult.ok(itemInfoVO);
    }

    //查询评价数量
    @GetMapping("/commentLevel")
    @ApiOperation(value = "查询评价数量",notes = "查询评价数量",httpMethod = "GET")
    public JSONResult getComments(String itemId){
        if(StringUtils.isBlank(itemId)){
            return JSONResult.errorMsg("商品Id不允许为null");
        }
        CommentCountsVO commentCountsVO = itemService.queryComments(itemId);
        return JSONResult.ok(commentCountsVO);
    }
    //查询评价信息列表
    @GetMapping("/comments")
    @ApiOperation(value = "查询评价信息",notes = "查询评价信息",httpMethod = "GET")
    public JSONResult getCommentsContents(CommentBO commentBO){
        if(StringUtils.isBlank(commentBO.getItemId())){
            return JSONResult.errorMsg("商品Id不允许为null");
        }

        if(commentBO.getPage()==null){
            commentBO.setPage(1);
        }
        if(commentBO.getPageSize()==null){
            commentBO.setPageSize(COMMENT_PAGE_SIZE);
        }
        PagedGridResult pagedGridResult = itemService.queryContentsList(commentBO);
        return JSONResult.ok(pagedGridResult);
    }

    //页面搜索功能
    @GetMapping("/search")
    @ApiOperation(value = "页面搜索功能",notes = "页面搜索功能",httpMethod = "GET")
    public JSONResult search(
            @ApiParam(name = "keyWords",value = "搜索关键字")
            String keywords,
            @ApiParam(name = "sort",value = "排序规则")
            String sort,
            @ApiParam(name = "page",value = "页码值")
            Integer page,
            @ApiParam(name = "pageSize",value = "每页显示条数")
            Integer pageSize){

        if(page==null){
           page=1;
        }
        if(pageSize==null){
           pageSize=COMMENT_PAGE_SIZE;
        }
        PagedGridResult pagedGridResult = itemService.searchItems(keywords, sort, page, pageSize);
        return JSONResult.ok(pagedGridResult);
    }
    //根据三级分类id搜索商品信息
    @GetMapping("/catItems")
    @ApiOperation(value = "根据三级分类id搜索商品信息",notes = "根据三级分类id搜索商品信息",httpMethod = "GET")
    public JSONResult getCatItems(
            @ApiParam(name = "catId",value = "三级分类id",required = true)
            String catId,
            @ApiParam(name = "sort",value = "排序规则")
                    String sort,
            @ApiParam(name = "page",value = "页码值")
                    Integer page,
            @ApiParam(name = "pageSize",value = "每页显示条数")
                    Integer pageSize) {
        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=COMMENT_PAGE_SIZE;
        }
        PagedGridResult pagedGridResult = itemService.searchCatItems(catId, sort, page, pageSize);
        return JSONResult.ok(pagedGridResult);
    }


}
